// 演示 MultiQueryRetriever 基本使用
import { NomicEmbeddings } from "./utils/embed.js";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { TextLoader } from "langchain/document_loaders/fs/text";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { ScoreThresholdRetriever } from "langchain/retrievers/score_threshold";

const loader = new TextLoader("data/kong.txt");

const docs = await loader.load();

const splitter = new RecursiveCharacterTextSplitter({
  chunkSize: 64,
  chunkOverlap: 0,
});

const splittedDocs = await splitter.splitDocuments(docs);

const embeddings = new NomicEmbeddings(3);

const store = new MemoryVectorStore(embeddings);

await store.addDocuments(splittedDocs);

// const retriever = store.asRetriever(2); // 创建一个基础的检索器

// const res = await retriever.invoke("茴香豆是做什么用的？");

// console.log(res);

const r = ScoreThresholdRetriever.fromVectorStore(store, {
  minSimilarityScore: 0.72,
});
const res = await r.invoke("茴香豆是做什么用的？");
console.log(res);
